package com.wangsd.web.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wangsd.web.entity.User;
import com.wangsd.web.mapper.UserMapper;
import com.wangsd.web.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImp implements UserService {

    @Resource
    private UserMapper userMapper;

    private QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    /**
     * 注册用户
     * @param user 用户实体类
     * @return 格式化信息
     */
    @Override
    public boolean register(User user) {
        return userMapper.insert(user) > 0;
    }

    /**
     * 销毁账号
     * @param user 用户实体类
     * @return 格式化信息
     */
    @Override
    public boolean destroy(User user) {
        return userMapper.deleteById(user.getUserId()) > 0;
    }

    /**
     * 修改用户信息
     * @param user 用户实体类
     * @return 格式化信息
     */
    @Override
    public boolean modify(User user) {
        return userMapper.updateById(user) > 0;
    }

    /**
     * 查询用户信息
     * @param username 用户名
     * @return 格式化信息
     */
    @Override
    public User getUserInfo(String username) {
        queryWrapper.clear();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }
}
